package br.edu.com1263.dao;

import java.util.Collection;

import javax.persistence.Query;

import br.edu.com1263.model.Aluno;

public class AlunoDAOImpl extends AbstractDAO implements AlunoDAO{
	
	public AlunoDAOImpl(){

}

	

	@Override
	public Collection<Aluno> consultarTodos() {
		Query query = manager.createQuery("SELECT a FROM Aluno a");
		return query.getResultList();
	}

	@Override
	public void salvar(Aluno aluno) {
		manager.persist(aluno);
		
	}
	
	
	public void alterar(Aluno aluno) {
		manager.merge(aluno);
	}
    
    public void remover(Aluno aluno) {
		manager.remove(aluno);
	}

	@Override
	public Aluno consultar(Long id) {
		return manager.find(Aluno.class, id);
	}



	@Override
	public Aluno validar(String matricula, String senha) {
		Query query = manager.createQuery("SELECT a FROM Aluno a Where a.matricula = :matricula AND a.senha = :senha");
		query.setParameter("matricula", matricula);
		query.setParameter("senha", senha);
		Collection<Aluno> aluno = query.getResultList();
		if (aluno.size() == 1)
			return aluno.iterator().next();
		return null;
	}



	@Override
	public Aluno consultarMatricula(String matAluno) {
		Query query = manager.createQuery("SELECT a FROM Aluno a Where a.matricula = :matricula");
		query.setParameter("matricula", matAluno);
		return (Aluno) query.getSingleResult();
	}

}
